/*
{*******************************************************************}
{                                                                   }
{       KS-GANTT DotNet Library                                     }
{       Gantt-Charting Control for Windows Forms                    }
{                                                                   }
{       Copyright (c) 2009 - 2012 by Kroll-Software                 }
{       All Rights Reserved                                         }
{                                                                   }
{   This code is provided 'as is',                                  }
{   without warranties of any kind. Use at your own risk.           }                                                              
{                                                                   }
{   As a license holder of the KS-Gantt Control,                    }
{   you may freely redistribute executables based on                }
{   and created with these contents.                                }
{   You can modify the source code to meet your needs.              }
{                                                                   }
{   However, you must not publish, redistribute or transfer         }
{   this source code to 3rd parties under any conditions.           }
{                                                                   } 
{*******************************************************************}
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KS.Common;

namespace MiGuava.SqlServer
{
    public partial class DropDatabaseDialog : Form
    {
        bool bRunning = false;

        private bool m_Dropped = false;
        public bool Dropped
        {
            get
            {
                return m_Dropped;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeGanttDB()
        {
            return false;
        }

        private GanttSqlDB m_GanttDB = null;
        public GanttSqlDB GanttDB
        {
            get
            {
                return m_GanttDB;
            }
            set
            {
                m_GanttDB = value;
                if (m_GanttDB != null)
                {
                    FillProjects();                    
                }
            }
        }

        public DropDatabaseDialog()
        {
            InitializeComponent();

            lsvProjects.Columns.Add(Properties.Resources.ColumnProject, 80);
            lsvProjects.Columns.Add(Properties.Resources.ColumnDescription, 160);

            lsvProjects.Columns[0].Width = lsvProjects.Width / 2 - 16;
            lsvProjects.Columns[1].Width = lsvProjects.Width / 2 - 16;

            lsvProjects.FullRowSelect = true;
            this.FormClosing += new FormClosingEventHandler(DropDatabaseDialog_FormClosing);

            lsvProjects.SelectedIndexChanged += new EventHandler(lsvProjects_SelectedIndexChanged);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            if (lsvProjects.Columns.Count > 1)
            {
                lsvProjects.Columns[0].Width = lsvProjects.Width / 2 - 16;
                lsvProjects.Columns[1].Width = lsvProjects.Width / 2 - 16;
            }
        }

        void lsvProjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            lsvProjects.SelectedIndices.Clear();
        }

        void DropDatabaseDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bRunning)
                e.Cancel = true;
        }

        private void DropDatabaseDialog_Load(object sender, EventArgs e)
        {
            lblInfo.Text = Properties.Resources.SQLDropDatabaseInfo;
        }

        private void FillProjects()
        {
            lblDatabase.Text = String.Format(Properties.Resources.SQLDatabaseServerCaption, m_GanttDB.DBCatalog, m_GanttDB.DBServer);

            lsvProjects.Items.Clear();            

            System.Data.SqlClient.SqlDataReader dr = null;

            try
            {
                dr = m_GanttDB.ExecuteSQL("select ItemKey, ItemText, Description from Projects ORDER BY ItemText");                

                while (dr.Read())
                {
                    ListViewItem item = new ListViewItem(Strings.GetSafeString(dr["ItemText"]));
                    item.SubItems.Add(Strings.GetSafeString(dr["Description"]));
                    item.Tag = Strings.GetSafeString(dr["ItemKey"]);
                    lsvProjects.Items.Add(item);                    
                }

                dr.Close();                
            }
            catch (Exception ex)
            {
                Program.ErrMsgBox(ex.Message);                
                return;
            }
            finally
            {
                if (dr != null)
                    dr.Dispose();                
            }
        }

        private void cmdDrop_Click(object sender, EventArgs e)
        {
            switch (Program.QuestionMsgBox(Properties.Resources.SQLDropDatabaseQuestion))
            {
                case DialogResult.No:
                    this.DialogResult = DialogResult.No;
                    this.Close();
                    return;

                case DialogResult.Cancel:
                    return;
            }

            cmdDrop.Enabled = false;
            cmdCancel.Enabled = false;            

            string strCatalog = "[" + m_GanttDB.DBCatalog + "]";

            GanttSqlDB db = new GanttSqlDB();
            db.DBCatalog = "master";
            db.DBPassword = m_GanttDB.DBPassword;
            db.DBServer = m_GanttDB.DBServer;
            db.DBUser = m_GanttDB.DBUser;
            db.DBWinSecurity = m_GanttDB.DBWinSecurity;

            try
            {
                bRunning = true;
                Program.CursorWait();

                db.BaseConnect();
                db.DBConn.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(DBConn_InfoMessage);
                
                m_GanttDB.Disconnect();
                //while (m_GanttDB.DBConn.State != ConnectionState.Closed)
                //{
                //    System.Threading.Thread.Sleep(1);
                //    Application.DoEvents();
                //}

                System.Data.SqlClient.SqlConnection.ClearPool(m_GanttDB.DBConn);

                db.ExecuteUpdateSQL("DROP DATABASE " + strCatalog);
                m_Dropped = true;
            }
            catch (Exception ex)
            {
                Program.CursorDefault();
                Program.ErrMsgBox(ex.Message);

                cmdDrop.Enabled = true;
                cmdCancel.Enabled = true;
            }
            finally
            {
                if (db != null)
                    db.Dispose();

                Program.CursorDefault();
                bRunning = false;
            }

            if (m_Dropped)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        void DBConn_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
        {
            Program.ErrMsgBox(e.Message);
        }        
    }
}